<template>
  <div class="layout">
    <!-- 顶部导航 -->
    <el-header class="header">
      <div class="container">
        <div class="nav-content">
          <div class="logo">
            <h2>校园墙</h2>
          </div>
          
          <el-menu
            :default-active="$route.path"
            mode="horizontal"
            router
            class="nav-menu"
          >
            <el-menu-item index="/home">
              <el-icon><House /></el-icon>
              首页
            </el-menu-item>
            
            <el-menu-item index="/home?category=1">
              <el-icon><Reading /></el-icon>
              学习交流
            </el-menu-item>
            
            <el-menu-item index="/home?category=2">
              <el-icon><Camera /></el-icon>
              生活分享
            </el-menu-item>
            
            <el-menu-item index="/home?category=3">
              <el-icon><QuestionFilled /></el-icon>
              求助提问
            </el-menu-item>
            
            <el-menu-item index="/home?category=4">
              <el-icon><Star /></el-icon>
              社团活动
            </el-menu-item>
            
            <el-menu-item index="/home?category=5">
              <el-icon><ShoppingBag /></el-icon>
              二手交易
            </el-menu-item>
            
            <el-menu-item index="/lost-found">
              <el-icon><Search /></el-icon>
              失物招领
            </el-menu-item>
          </el-menu>

          <div class="nav-actions">
            <!-- 通知铃铛 -->
            <NotificationBell v-if="userStore.isLoggedIn" />
            
            <div v-if="userStore.isLoggedIn" class="user-menu">
              <el-dropdown>
                <span class="user-info">
                  <UserAvatar :user="userStore.userInfo" :size="32" />
                  <span class="username">{{ userStore.userInfo?.username }}</span>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="goToProfile">
                      <el-icon><User /></el-icon>
                      个人中心
                    </el-dropdown-item>
                    <el-dropdown-item @click="goToNotifications">
                      <el-icon><Bell /></el-icon>
                      通知中心
                    </el-dropdown-item>
                    <el-dropdown-item divided @click="handleLogout">
                      <el-icon><SwitchButton /></el-icon>
                      退出登录
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
            
            <div v-else class="auth-buttons">
              <el-button @click="goToLogin">登录</el-button>
              <el-button type="primary" @click="goToRegister">注册</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-header>

    <!-- 主要内容区域 -->
    <el-main class="main">
      <div class="container">
        <router-view />
      </div>
    </el-main>

    <!-- 底部 -->
    <el-footer class="footer">
      <div class="container">
        <p>&copy; 2024 校园墙. All rights reserved.</p>
      </div>
    </el-footer>
    
    <!-- 回到顶部按钮 -->
    <BackToTop />
  </div>
</template>

<script setup>
import { useUserStore } from '@/stores/user'
import { ElMessageBox, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { onMounted, watch } from 'vue'
import NotificationBell from '@/components/NotificationBell.vue'
import UserAvatar from '@/components/UserAvatar.vue'
import BackToTop from '@/components/BackToTop.vue'

const router = useRouter()
const userStore = useUserStore()

// 跳转到创建帖子页面
const goToCreate = () => {
  router.push('/post/create')
}

// 跳转到个人中心
const goToProfile = () => {
  router.push('/profile')
}

// 跳转到通知中心
const goToNotifications = () => {
  router.push('/notifications')
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确认退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    
    userStore.logout()
    router.push('/home')
    ElMessage.success('已退出登录')
  } catch {
    // 用户取消
  }
}

// 初始化时获取用户信息
onMounted(() => {
  if (userStore.isLoggedIn && !userStore.userInfo) {
    userStore.fetchUserInfo()
  }
})

// 监听登录状态变化
watch(() => userStore.isLoggedIn, (newValue) => {
  if (newValue && !userStore.userInfo) {
    userStore.fetchUserInfo()
  }
}, { immediate: true })
</script>

<style scoped>
.layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
}

.header {
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(20px);
  border-bottom: 1px solid rgba(226, 232, 240, 0.6);
  padding: 0;
  height: 70px !important;
  line-height: 70px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.04);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.nav-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
}

.logo h2 {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0;
  font-weight: 700;
  font-size: 24px;
  letter-spacing: -0.5px;
}

.nav-menu {
  border-bottom: none;
  flex: 1;
  margin: 0 40px;
  background: transparent;
}

.nav-menu .el-menu-item {
  height: 50px;
  line-height: 50px;
  margin: 0 8px;
  border-radius: 12px;
  color: #64748b;
  font-weight: 500;
  font-size: 15px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  position: relative;
  overflow: hidden;
}

.nav-menu .el-menu-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(102, 126, 234, 0.1), transparent);
  transition: left 0.5s ease;
}

.nav-menu .el-menu-item:hover::before {
  left: 100%;
}

.nav-menu .el-menu-item:hover,
.nav-menu .el-menu-item.is-active {
  background: rgba(102, 126, 234, 0.1);
  color: #667eea;
  border-bottom: none;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.nav-menu .el-menu-item .el-icon {
  margin-right: 8px;
  font-size: 18px;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 50px;
}

.nav-actions .el-button {
  height: 40px;
  padding: 0 20px;
  border-radius: 20px;
  font-weight: 500;
  letter-spacing: 0.3px;
}

.nav-actions .el-button--primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

.nav-actions .el-button--primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.user-menu .user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 16px;
  transition: all 0.3s ease;
  height: 50px;
  line-height: 50px;
  box-sizing: border-box;
  transform: translateY(7px);
}

.user-menu .user-info:hover {
  background: rgba(102, 126, 234, 0.1);
}

.username {
  font-size: 14px;
  color: #475569;
  font-weight: 500;
}

.auth-buttons {
  display: flex;
  gap: 12px;
}

.auth-buttons .el-button {
  height: 36px;
  padding: 0 16px;
  border-radius: 18px;
}

.main {
  flex: 1;
  padding: 32px 0;
  background: transparent;
}

.footer {
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(20px);
  border-top: 1px solid rgba(226, 232, 240, 0.6);
  text-align: center;
  color: #64748b;
  padding: 24px 0;
  height: auto !important;
  font-weight: 500;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.04);
}

/* 动画效果 */
@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.nav-menu .el-menu-item {
  animation: slideIn 0.3s ease forwards;
}

.nav-menu .el-menu-item:nth-child(1) { animation-delay: 0.1s; }
.nav-menu .el-menu-item:nth-child(2) { animation-delay: 0.2s; }
.nav-menu .el-menu-item:nth-child(3) { animation-delay: 0.3s; }
.nav-menu .el-menu-item:nth-child(4) { animation-delay: 0.4s; }
.nav-menu .el-menu-item:nth-child(5) { animation-delay: 0.5s; }
.nav-menu .el-menu-item:nth-child(6) { animation-delay: 0.6s; }

/* 响应式设计 */
@media (max-width: 1200px) {
  .nav-menu {
    margin: 0 20px;
  }
  
  .nav-menu .el-menu-item {
    margin: 0 4px;
    font-size: 14px;
  }
}

@media (max-width: 768px) {
  .header {
    height: 60px !important;
    line-height: 60px;
  }
  
  .logo h2 {
    font-size: 20px;
  }
  
  .nav-menu {
    margin: 0 10px;
  }
  
  .nav-menu .el-menu-item {
    height: 40px;
    line-height: 40px;
    margin: 0 2px;
    font-size: 12px;
    padding: 0 8px;
  }
  
  .nav-actions {
    gap: 8px;
  }
  
  .username {
    display: none;
  }
}
</style>